﻿using System.Threading;

namespace Blog.Core.Api.HostServices
{
    public class TimeHostService : BackgroundService
    {
        /// <summary>
        /// 定时器
        /// </summary>
        private Timer _time;
        /// <summary>
        /// 执行服务
        /// </summary>
        /// <param name="stoppingToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>

        protected override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            //int i = 1;
            ////如果没停止，就一直执行
            //while (!stoppingToken.IsCancellationRequested)
            //{
            //    Writerto(i++);
            //    if (i == 100)//当执行次数等于100时
            //    {
            //        //跳出循环
            //        break;
            //    }
            //}
            _time = new Timer(Writerto, null, TimeSpan.Zero, TimeSpan.FromMinutes(1));
            return Task.CompletedTask;
        }
        /// <summary>
        /// 输出一句话
        /// </summary>
        /// <param name="i"></param>
        private void Writerto(object state)
        {
            Console.WriteLine("傻蛋");
        }
    }
}
